<?php
class AdController extends Yaf_Controller_Abstract {

   	public function init(){
         if(is_null(Yaf_Session::getInstance()->get(AK47))){
            $this->redirect('/index.php/admin/login/login');
         }
         if ($this->getRequest()->isXmlHttpRequest()) {
            Yaf_Dispatcher::getInstance()->disableView();
         }
   		$this->setViewPath(APP_PATH . '/application/modules/Admin/views');
         $data = array(
               'right_title'  =>'广告管理',
               "title"        => '惠信后台管理',
               'ad_li_active' =>"class='active'",
               'content_nav'  =>array(
                  
                  array('name'=>'广告列表','href'=>'/index.php/admin/ad/index'),
                  array('name'=>'广告添加','href'=>'/index.php/admin/ad/add'),
                  array('name'=>'广告位列表','href'=>'/index.php/admin/ad/typeindex'),
                  array('name'=>'广告位添加','href'=>'/index.php/admin/ad/typeadd'),
               )
            );
         $this->getView()->assign($data);
   	}

      /**
       * 广告列表
       * @return [type] [description]
       */
      public function indexAction(){
         $ad_model = new adModel();
         $ad_type_model = new adtypeModel();
         $ad_list = $ad_model->select();
         //trun type id into type name
         foreach ($ad_list as $key => $value) {
            $temp = $ad_type_model->select(array('_id'=> new mongoId($value['type_id'])));
            if(isset($temp[0])){
               $ad_list[$key]['adtype_name'] = $temp[0]['name'];
            }else{
               $ad_list[$key]['adtype_name'] = '<font style="color:red;">Unknow type</font>';
            }
         }
         $data = array(
            'active' => 0,
            'ad_list'=>$ad_list
         );
         $this->getView()->assign($data);
      }

      public function addAction(){
         $post_data = $this->getRequest()->getPost();
         if(empty($post_data['addcheck'])){
            $adtype_model = new adtypeModel();
            $adtype_list = $adtype_model->select(array('status'=>'1'));
            $data = array(
               'active' => 1,
               'adtype_list' => $adtype_list
            );
            $this->getView()->assign($data);   
         }else{
            unset($post_data['addcheck']);

            //get the upload file and move it to the right place
            if(!is_uploaded_file($_FILES['pic']['tmp_name'])){
               $err_msg = "Is not a uploaded file!";
            }
            //Set the new name of the uploaded file
            $pic_name = $post_data['type_id'].'-'.time(). '-' . $_FILES['pic']['name'];
            $pic_dir_path = Yaf_Application::app()->getConfig()->ad->pic_postion;
            if(!file_exists($pic_dir_path)){
               //if the path's dir is not exist,make it by set power 0777 and set the third param of mkdir for true 
               mkdir($pic_dir_path,0777,true);
            }
            if(move_uploaded_file($_FILES['pic']['tmp_name'], $pic_dir_path . '/' . $pic_name)){

               $err_msg = "File move failed!";
            }

            $post_data['pic'] = $pic_name;
            $ad_model = new adModel();
            $res = $ad_model->insert($post_data);
            if(isset($res) && ($res['ok'] == 1)){
               $err_msg = '添加成功';
            }else{
               $err_msg = '添加失败';
            }
            $adtype_model = new adtypeModel();
            $adtype_list = $adtype_model->select(array('status'=>'1'));
            $data = array(
               'active' => 1,
               'err_msg' => $err_msg,
               'adtype_list' => $adtype_list
            );
            $this->getView()->assign($data);    //no testing   waiting testing!
         }
      }

      public function typeindexAction(){
         $ad_type_model = new adtypeModel();
         $ad_type_list = $ad_type_model->select();
         $data = array(
            'active' => 2,
            'list' => $ad_type_list
         );
         $this->getView()->assign($data); 
      }

      public function typeaddAction(){
         $data = array(
         'active' => 3,

         );
         $this->getView()->assign($data); 
         
      }

      public function ajax_typeaddAction(){
         $post_data = $this->getRequest()->getPost();
         $ad_type_model = new adtypeModel();
         $res = $ad_type_model->insert($post_data);
         if(isset($res) && ($res['ok'] == 1)){
            exit(json_encode(array('flag'=>true,'msg'=>'添加成功')));
         }else{
            exit(json_encode(array('flag'=>false,'msg'=>'添加失败')));
         }
      }

      public function ajax_typedelAction(){
         $_id = $this->getRequest()->getQuery('_id');
         $ad_type_model = new adtypeModel();
         $del_res = $ad_type_model->delete(array('_id'=>$_id));
         if(isset($del_res) && ($del_res['ok'] == 1)){
            exit(
                  json_encode(array('flag'=>true,'msg'=>$_id))
               );
         }
         exit(
                  json_encode(array('flag'=>false,'msg'=>'删除失败'))
               );
      }

     public function ajax_addelAction(){
         $_id = $this->getRequest()->getQuery('_id');
         $ad_model = new adModel();
         $res = $ad_model->select(array('_id'=> new mongoId($_id)));
         $del_res = $ad_model->delete(array('_id'=>$_id));
         if(isset($del_res) && ($del_res['ok'] == 1)){
            //if delete success, then ,delete its images
            $image_path = Yaf_Application::app()->getConfig()->ad->pic_postion . '/' . $res[0]['pic'];
            if(isset($res[0]) && file_exists($image_path))
               unlink($image_path);

            exit(
                  json_encode(array('flag'=>true,'msg'=>$_id))
               );
         }
         exit(
                  json_encode(array('flag'=>false,'msg'=>'删除失败'))
               );
      } 
}

?>